package cn.tedu.apiday10;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//按行读写
public class Server2 {
    public static void main(String[] args) throws IOException {
        //启动服务器
        ServerSocket serverSocket = new ServerSocket(8000);
        System.out.println("服务器启动");
        //接收客户端的连接请求
        while (true) {
            Socket socket = serverSocket.accept();
            //读取客户端发来的数据 ---in---按行读BufferedReader
            InputStream inputStream = socket.getInputStream();
            //创建按行读的工具类。需要把字节流in转成字符流作为参数构建BufferedReader
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = reader.readLine();//按行读取数据
            System.out.println("客户端发来的数据是" + line);
            //开始写出数据
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream);
            System.out.println("请输入想要发给客户端的东西");
            String input = new Scanner(System.in).nextLine();
            writer.println(input);//按行写出
            writer.flush();//把数据刷出去
        }
    }
}
